package cn.xopencode.recordservice.dal.mysql.mapper.lowroom;

import cn.xopencode.common.framework.util.DateUtil;
import cn.xopencode.mybatis.core.query.QueryWrapperX;
import cn.xopencode.recordservice.dal.mysql.dataobject.lowroom.LowRoomRecordDO;
import cn.xopencode.recordservice.service.lowroom.bo.LowRoomRecordPageBO;
import cn.xopencode.recordservice.service.robotroom.bo.AdminRecordPageBO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface LowRoomRecordMapper extends BaseMapper<LowRoomRecordDO> {

    default IPage<LowRoomRecordDO> selectPage(LowRoomRecordPageBO pageBO) {
        if(pageBO.getInspectionTime() != null){
            String inspectionTime = DateUtil.format(pageBO.getInspectionTime(), "yyyy-MM-dd");
            return selectPage(new Page<>(pageBO.getPageNo(), pageBO.getPageSize()),
                    new QueryWrapperX<LowRoomRecordDO>()
                            .betweenIfPresent("inspection_time",inspectionTime + " 00:00:00",inspectionTime + " 23:59:59")
                            .orderByDesc(" is_notice_leader = 1 and is_leader_deal = 0 ")
                            .orderByDesc("inspection_time"));
        }
        return selectPage(new Page<>(pageBO.getPageNo(), pageBO.getPageSize()),
                new QueryWrapperX<LowRoomRecordDO>().orderByDesc(" is_notice_leader = 1 and is_leader_deal = 0 ")
                        .orderByDesc("inspection_time"));
    }

    /**
     * 获取最新的一条记录
     * @return
     */
    default LowRoomRecordDO getLastRecord(){
        return selectOne(new QueryWrapperX<LowRoomRecordDO>()
                .orderByDesc("inspection_time").last("limit 1"));
    }

    /**
     * 后台按条件分页查询列表数据
     * @param pageBO
     * @return
     */
    IPage<LowRoomRecordDO> adminQuery(Page page, @Param("pageBO") AdminRecordPageBO pageBO);

}
